home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / pccts.zip / unbag.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  2KB  |  94 lines

  1. /*
  2.  * unbag
  3.  *
  4.  * This program "unbags" files "bagged" by bag.  Can be used for non-UNIX
  5.  * folks that need to unbag stuff.
  6.  */
  7. #include <stdio.h>
  8.  
  9. static int line=0;
  10.  
  11. main(argc, argv)
  12. int argc;
  13. char **argv;
  14. {
  15.     FILE *f;
  16.  
  17.     if ( argc <= 1 )
  18.     {
  19.         fprintf(stderr, "unbag: missing bagfile name\n");
  20.         exit(-1);
  21.     }
  22.     f = fopen(argv[1], "r");
  23.     if ( f == NULL )
  24.     {
  25.                 fprintf(stderr, "unbag: cannot open %s\n", argv[1]);
  26.         exit(-1);
  27.     }
  28.  
  29.     unbag( f );
  30.  
  31.     fclose( f );
  32. }
  33.  
  34. unbag(f)
  35. FILE *f;
  36. {
  37.     FILE *output;
  38.     static char text[2048];
  39.     char *nm, *p;
  40.  
  41.     while ( fgets(text, 2048, f) != NULL )
  42.     {
  43.         line++;
  44.         if ( strncmp(text, "cat << \\EOF_", strlen("cat << \\EOF_"))!= 0 )
  45.         {
  46.             fprintf(stderr, "unbag: line %d: bad file format (missing BEGIN)\n", line);
  47.             fprintf(stderr, "unbag: text was '%s'\n", text);
  48.             exit(-1);
  49.         }
  50.         nm = &text[strlen("cat << \\EOF_")];
  51.         for (p=nm; *p!=' '; p++) {;}    /* find end of filename */
  52.         *p = '\0';
  53.         output = fopen(nm, "w");
  54.         if ( output == NULL )
  55.         {
  56.             fprintf(stderr, "unbag: cannot open ouput file %s\n", nm);
  57.             exit(-1);
  58.         }
  59.         extract(f, output, nm);
  60.         fclose(output);
  61.     }
  62. }
  63.  
  64. /* Cat a f to f2 (lines <= 2047 characters) stopping after reading 'stop' */
  65. extract(f, f2, stop)
  66. FILE *f, *f2;
  67. char *stop;
  68. {
  69.     static char text[2048];
  70.     char *p;
  71.  
  72.     while ( fgets(text, 2048, f)!=NULL )
  73.     {
  74.         line++;
  75.         text[strlen(text)-1] = '\0';    /* rm \n */
  76.         if ( strncmp(text, "EOF_", strlen("EOF_")) == 0 )
  77.             if ( strcmp(&text[strlen("EOF_")], stop) == 0 ) return;
  78.         if ( text[0] != '>' )
  79.         {
  80.             fprintf(stderr,"unbag: line %d: bad file format: %s\n", line, stop);
  81.             exit(-1);
  82.         }
  83.         fprintf(f2, "%s\n", &text[1]);
  84. /*
  85.         for (p=&text[1]; *p!='\0'; p++)
  86.         {
  87.             if ( *p=='\\' ) p++;
  88.             putc(*p, f2);
  89.         }
  90.         putc('\n', f2);
  91. */
  92.     }
  93. }
  94.